package cece.service.impl;

import cece.field.MovieField;
import cece.service.SearchService;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SearchServiceImpl implements SearchService {

    @Autowired
    private ElasticsearchTemplate template;


    @Override
    public List<MovieField> keyWordSearch(String keyWord) {
        NativeSearchQuery query = new NativeSearchQueryBuilder().
                withQuery(QueryBuilders.queryStringQuery(keyWord)).
                withPageable(PageRequest.of(0,8)).
                build();
        List<MovieField> movieFields = template.queryForList(query, MovieField.class);
        return movieFields;
    }
}
